class Account < ActiveRecord::Base

  def load
    content = open("http://myspace.com/#{self.name}").read    
    self.udate = Time.now
    self.save
  end
  
  def parse
    myspaceComment = Scraper.define do
      process 'td:first-child>a:first-child', :pseudo=>:text
      process 'td:nth-child(2)>span', :cdate=>:text
      process 'td:nth-child(2)', :content=>:text
      result :pseudo, :cdate, :content
    end
    myspaceComments = Scraper.define do
      array :comments
      process 'tr', :comments=>myspaceComment      
      result :comments
    end
    myspace = Scraper.define do
      process 'table[align="center"]>tr:nth-child(2)',  :comments=>myspaceComments
      result :comments
    end
    return myspace.scrape(self.content)    
  end
  
  def reload(force = false)
    if self.content.nil? or self.udate < ( Time.now - 30.minutes) 
      self.load
    end
  end
    
  def after_create
    self.load
  end
  
end
